package com.jy.fxf_phone;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.jy.fxf_phone.service.BGMService;
import com.jy.fxf_phone.utils.BleUtils;
import com.jy.fxf_phone.view.AppFragment;
import com.jy.fxf_phone.view.FaceFragment;
import com.jy.fxf_phone.view.FeedActivity;
import com.jy.fxf_phone.view.MoveActivity;
import com.jy.fxf_phone.view.MusicFragment;
import com.jy.fxf_phone.view.SettingActivity;
import com.jy.fxf_phone.view.VideoActivity;
import com.jy.fxf_phone.view.VoiceFragment;
import com.jy.fxf_phone.widget.FoodDropView;
import com.jy.fxf_phone.widget.SlidingMenu;

import static com.jy.fxf_phone.R.id.tl;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private SlidingMenu mMenu;
    Toolbar toolbar;
    TextView tvTitle;
    ViewPager vp;
    Fragment fragment;
    FragmentPagerAdapter adapter;
    Button btnApp, btnMusic, btnVoice, btnFace;
    Button btnStore, btnSetting, btnVideo;
    ImageButton ibMove;
    ImageButton ibFeed;

    FoodDropView mFoodDropView;

    SharedPreferences sharedPreferences;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


//        linBle();

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initViews();


        toolbar.setTitle("");
        toolbar.setTitleTextColor(ContextCompat.getColor(this, R.color.toolbarTitleText));
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.drawable.ico_nav);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMenu.toggle();
            }
        });
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                linBle();
                return false;
            }
        });
        adapter = new FragmentAdapter(getSupportFragmentManager());
        vp.setAdapter(adapter);
        vp.setCurrentItem(0);
        tvTitle.setText("语音");
        setButtonSelect(btnVoice);

        sharedPreferences = getSharedPreferences("Sound", MODE_PRIVATE);
        int isOpen = sharedPreferences.getInt("sound", -1);//0开，1关
        if (isOpen == -1 || isOpen == 0) {
            startService(new Intent(this, BGMService.class));
        } else if (isOpen == 1) {
            stopService(new Intent(this, BGMService.class));
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        mFoodDropView.startFoodDrop();
    }

    private void linBle() {
        BleUtils.getInstance().initBle(this);
        BleUtils.getInstance().scanBleDevices(true);
    }

    @Override
    public void onClick(View v) {
        setButtonSelect(v);
        switch (v.getId()) {
            case R.id.btn_Nav_App:
                vp.setCurrentItem(3);
                break;
            case R.id.btn_Nav_Music:
                vp.setCurrentItem(2);
                break;
            case R.id.btn_Nav_Voice:
                vp.setCurrentItem(0);
                break;
            case R.id.btn_Nav_Face:
                vp.setCurrentItem(1);
                break;
            case R.id.ib_Nav_Move:
                Intent intent = new Intent(this, MoveActivity.class);
                startActivity(intent);
                overridePendingTransition(R.anim.ani_activity_open, 0);
                break;
            case R.id.ib_Feed:
                Intent intent2 = new Intent(this, FeedActivity.class);
                startActivity(intent2);
                break;
            case R.id.btnSetting:
                startActivity(new Intent(this, SettingActivity.class));
                mMenu.closeMenu();
                break;
            case R.id.btnVideo:
                startActivity(new Intent(this, VideoActivity.class));
                break;
        }
    }


    class FragmentAdapter extends FragmentPagerAdapter {

        public FragmentAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    fragment = new VoiceFragment();
                    break;
                case 1:
                    fragment = new FaceFragment();
                    break;
                case 2:
                    fragment = new MusicFragment();
                    break;
                case 3:
                    fragment = new AppFragment();
                    break;
            }
            return fragment;
        }

        @Override
        public int getCount() {
            return 4;
        }
    }



    public void setButtonSelect(View v) {
        v.setSelected(true);
        switch (v.getId()) {
            case R.id.btn_Nav_App:
                btnMusic.setSelected(false);
                btnVoice.setSelected(false);
                btnFace.setSelected(false);
                tvTitle.setText("应用中心");
                break;
            case R.id.btn_Nav_Music:
                btnApp.setSelected(false);
                btnVoice.setSelected(false);
                btnFace.setSelected(false);
                tvTitle.setText("音乐");
                break;
            case R.id.btn_Nav_Voice:
                btnApp.setSelected(false);
                btnMusic.setSelected(false);
                btnFace.setSelected(false);
                tvTitle.setText("语音");
                break;
            case R.id.btn_Nav_Face:
                btnApp.setSelected(false);
                btnMusic.setSelected(false);
                btnVoice.setSelected(false);
                tvTitle.setText("表情");
                break;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mFoodDropView.unsubscribeSubscription();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BleUtils.getInstance().closeBle();
        stopService(new Intent(this, BGMService.class));
    }

    private void initViews() {
        toolbar = (Toolbar) findViewById(tl);
        tvTitle= (TextView) findViewById(R.id.tvTitle);
        mMenu = (SlidingMenu) findViewById(R.id.id_menu);
        vp = (ViewPager) findViewById(R.id.vp);
        btnApp = (Button) findViewById(R.id.btn_Nav_App);
        btnMusic = (Button) findViewById(R.id.btn_Nav_Music);
        btnVoice = (Button) findViewById(R.id.btn_Nav_Voice);
        btnFace = (Button) findViewById(R.id.btn_Nav_Face);
        btnStore = (Button) findViewById(R.id.include_menu).findViewById(R.id.btnStore);
        btnSetting = (Button) findViewById(R.id.include_menu).findViewById(R.id.btnSetting);
        btnVideo = (Button) findViewById(R.id.include_menu).findViewById(R.id.btnVideo);
        ibMove = (ImageButton) findViewById(R.id.ib_Nav_Move);
        ibFeed = (ImageButton) findViewById(R.id.ib_Feed);

        mFoodDropView = (FoodDropView) findViewById(R.id.food_drop_view);

        mMenu.setHorizontalFadingEdgeEnabled(false);

        btnApp.setOnClickListener(this);
        btnMusic.setOnClickListener(this);
        btnVoice.setOnClickListener(this);
        btnVideo.setOnClickListener(this);
        btnFace.setOnClickListener(this);
        ibMove.setOnClickListener(this);
        ibFeed.setOnClickListener(this);
        btnSetting.setOnClickListener(this);
    }
}
